package bb.lanxing.manager;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.baidu.platform.comapi.map.MapBundleKey;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.List;


import bb.lanxing.App;
import bb.lanxing.model.database.LevelHP;
import bb.lanxing.model.database.User;
import bb.lanxing.model.json.level.LevelRule;
import bb.lanxing.network.MyHttpClient;
import bb.lanxing.network.NetSubscribe;
import bb.lanxing.util.AppFileUtils;
import bb.lanxing.util.JsonUtil;
import bb.lanxing.util.Log;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class LevelPermissionMgr {
    final static private String TAG = "LevelPermissionMgr";

    private static volatile LevelPermissionMgr instance;
    private String levelCachePath;
    private LevelHP levelHP;
    private LevelRule levelRule;

    public boolean checkPermission(String str) {
        return true;
    }

    private LevelPermissionMgr() {
    }

    public static LevelPermissionMgr getInstance() {
        if (instance == null) {
            synchronized (LevelPermissionMgr.class) {
                if (instance == null) {
                    instance = new LevelPermissionMgr();
                }
            }
        }
        return instance;
    }

    public void init() {
        loadRule();
    }

    private void loadRule() {
        loadCacheRule();
    }

    private void loadCacheRule() {
        String fileFromAssets;
        String str = AppFileUtils.buildExternalDirectoryPath(MapBundleKey.MapObjKey.OBJ_LEVEL) + File.separatorChar + "PermissionCache.json";
        this.levelCachePath = str;
        if (AppFileUtils.isFileExists(str)) {
            fileFromAssets = AppFileUtils.readFile(this.levelCachePath);
        } else {
            fileFromAssets = AppFileUtils.getFileFromAssets("level/PermissionCache.json");
        }
        try {
            if (TextUtils.isEmpty(fileFromAssets)) {
                return;
            }
            this.levelRule = JSON.parseObject(fileFromAssets, LevelRule.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public LevelRule getRule() {
        return this.levelRule;
    }

    public LevelHP getLevelHP() {
        return this.levelHP;
    }

    public void loadLevelHP() {
        loadCacheLevelHP();
        requestLevelHP();
    }

    private void loadCacheLevelHP() {
        User signinUser = App.getContext().getSigninUser();
        if (signinUser != null) {
            Long id = signinUser.getId();
            if (null != id) {
                this.levelHP = LevelHP.getByUserId(id);
            }
        }
    }

    public void requestLevelHP() {
        final User signinUser = App.getContext().getSigninUser();
        if (signinUser == null) {
            return;
        }
        Observable.create(new NetSubscribe(MyHttpClient.requestLevelHP())).subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).map(str -> {
            JSONException e;
            LevelHP levelHP;
            try {
                levelHP = JSON.parseObject(JsonUtil.getStringValue("data", new JSONObject(str)), LevelHP.class);
                if (levelHP != null) {
                        levelHP.setUserId(signinUser.getId());
                    levelHP.setUpdateTime(System.currentTimeMillis());
                    levelHP.save();
                }
                Log.v(TAG, "levelHP = " + levelHP);
            } catch (JSONException e3) {
                e = e3;
                levelHP = null;
            }
            return levelHP;
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable th) {
            }

            @Override
            public void onNext(LevelHP levelHP) {
                LevelPermissionMgr.this.levelHP = levelHP;
            }
        });
    }

    public boolean checkPermission(int i) {
        LevelHP levelHP = this.levelHP;
        if (levelHP == null || levelHP.isExpired()) {
            requestLevelHP();
            return true;
        }
        boolean result = false;
        Log.v(TAG, "checkPermission, hasPermission = " + result + ", levelHP = " + this.levelHP);
        return result;
    }

    public void updatePermisstion(int i) {
        int bangCreate;
        if (i == 1) {
            int workoutExport = this.levelHP.getWorkoutExport();
            if (workoutExport > 0) {
                this.levelHP.setWorkoutExport(workoutExport - 1);
                this.levelHP.save();
            }
        } else if (i == 2) {
            int workout3D = this.levelHP.getWorkout3D();
            if (workout3D > 0) {
                this.levelHP.setWorkout3D(workout3D - 1);
                this.levelHP.save();
            }
        } else if (i == 18) {
            int lushuDownload = this.levelHP.getLushuDownload();
            if (lushuDownload > 0) {
                this.levelHP.setLushuDownload(lushuDownload - 1);
                this.levelHP.save();
            }
        } else if (i != 34) {
            if (i == 65 && (bangCreate = this.levelHP.getBangCreate()) > 0) {
                this.levelHP.setBangCreate(bangCreate - 1);
                this.levelHP.save();
            }
        } else {
            int clubCreate = this.levelHP.getClubCreate();
            if (clubCreate > 0) {
                this.levelHP.setClubCreate(clubCreate - 1);
                this.levelHP.save();
            }
        }
    }

    public int getCanHangMedalCount(int i) {
        List<Integer> medalHangCountList;
        LevelRule levelRule = this.levelRule;
        if (levelRule == null || (medalHangCountList = levelRule.getMedalHangCountList()) == null) {
            return 0;
        }
        if (i < 16) {
            return medalHangCountList.get(i);
        }
        return medalHangCountList.get(15);
    }
}